<template>
  <!--基本信息  -->
  <div>
    <FormPanel
      ref="formPanel"
      :list="initForm"
      :cols="2"
      :formType="type"
      :labelWidth="110"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button
        v-if="type !== 'show'"
        :loading="saveLoading"
        @click="saveDialog"
        type="primary"
      >{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import { authority } from '@/utils/authMixin';
import { dictionary } from '@/utils/dictMixin';
import { updateContactInfo } from '@/api/cms-api/customerManage/continueManage/record/index';
import { addContactInfo } from '@/api/cms-api/customerManage/continueManage/record/index';
export default {
  mixins: [authority, dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改' };
    this.saveData = null;
    return {
      contactId: this.rowData['contactId'],
      projectId: this.rowData['projectId'],
      initForm: this.initFormFun(),
      saveLoading: false,
      formValue: {}
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      console.log(this.type);
      if (this.type === 'edit') {
        this.initForm.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    initFormFun() {
      return [
        {
          type: 'SELECT',
          label: '联系人类型:',
          fieldName: 'contactType',
          itemList: this.createDictList('2517'),
          rules: [{ required: true, message: '联系人类型必选', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '姓名:',
          fieldName: 'contactName',
          disabled: false,
          rules: [{ required: true, message: '姓名必填', trigger: 'blur' }, { max: 10, message: '姓名长度不能超过10', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '性别:',
          fieldName: 'contactSex',
          rules: [{ required: true, message: '性别必选', trigger: 'blur' }],
          itemList: this.createDictList('1002')
        },
        {
          type: 'INPUT',
          label: '职务:',
          fieldName: 'contactJob',
          disabled: false,
          rules: [{ max: 20, message: '职务长度不能超过20', trigger: 'blur' }]
        },

        {
          type: 'INPUT',
          label: '手机',
          fieldName: 'contactMobile',
          rules: [{ required: true, message: '手机必填', trigger: 'blur' }, { pattern: /^[1][3,4,5,7,8][0-9]{9}$/, message: '请填写正确的手机格式', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '邮箱:',
          fieldName: 'contactEmail',
          disabled: false,
          rules: [{ pattern: /^([-_a-zA-Z0-9\u4e00-\u9fa5\.])+@([-_a-zA-Z0-9])+(\.[a-zA-Z]{2,5}){1,3}$/, message: '请填写正确的邮箱格式', trigger: 'change' }]
        },
        {
          type: 'DATE',
          label: '出生日期:',
          fieldName: 'birthday',
          valueFormat: 'yyyy-MM-dd'
        },
        {
          type: 'INPUT',
          label: '家庭住址:',
          fieldName: 'homeAddress',
          disabled: false,
          rules: [{ max: 50, message: '家庭住址长度不能超过50', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '个人喜好:',
          fieldName: 'hobby',
          disabled: false,
          rules: [{ max: 100, message: '个人喜好长度不能超过100', trigger: 'blur' }]
        }
      ];
    },
    async formChangeHandle(val) {
      this.formValue = val;
      if (this.formValue !== null) {
        this.saveLoading = true;
        let res = {};
        console.log('2222----', this.formValue);
        this.formValue = Object.assign({}, { projectId: this.projectId }, this.formValue);
        if (this.type === 'add') {
          res = await addContactInfo(this.formValue);
        }
        if (this.type === 'edit') {
          res = await updateContactInfo(this.formValue, this.contactId);
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
        }
      }
    },
    saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>
<style scoped>
.wrapper {
  height: calc(100vh - 110px);
}
</style>
